﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.DAO.Admin;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.BO.Admin
{
    public class MenuBO : BaseBO
    {
        public const string FIELD_MENU_ID = "MenuId";
        public const string FIELD_MENU_NAME = "MenuName";
        public const string FIELD_MENU_CODE = "MenuCode";
        public const string FIELD_MENU_DESCRIPTION = "MenuDescription";
        public const string FIELD_MENU_URL = "MenuUrl";
        public const string FIELD_APP_ID = "AppId";
        public const string FIELD_APP_NAME = "AppName";
        public const string FIELD_PARENT_MENU_ID = "ParentMenuId";
        public const string FIELD_IS_ACTIVE = "IsActive";
        public const string FIELD_RECORD_COUNT = "RecordCount";
        public const string FIELD_ROLE_ID = "RoleId";
        public const string FIELD_CLASS_NAME = "ClassName";
        public const string FIELD_MENU_ORDER = "MenuOrder";

        private String _connectionString;
        
        public MenuBO(string connString)
        {
            this._connectionString = connString;
        }
        public MenuBO()
        {
            this._connectionString = Common.ConnectionUtil.DEFAULT_CONN_STRING;
        }

        public MenuDAO _menuDAO;

        public MenuDAO CreateSQLDAO()
        {
            return new MenuDAO(_connectionString, DEFAULT_MAPPER_TYPE);
        }

        public DataPage<MenuModel> GetDynamicListPaging(MenuModel aModel, int pageSize, int pageIndex, bool compareEqual)
        {
            try
            {
                _menuDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {MenuModel.FIELD_MENU_ID,FIELD_MENU_ID},
                    {MenuModel.FIELD_MENU_NAME,FIELD_MENU_NAME},
                    {MenuModel.FIELD_MENU_CODE,FIELD_MENU_CODE},  
                    {MenuModel.FIELD_MENU_DESCRIPTION,FIELD_MENU_DESCRIPTION},
                    {MenuModel.FIELD_MENU_URL,FIELD_MENU_URL},
                    {MenuModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE},
                    {MenuModel.FIELD_APP_ID,FIELD_APP_ID},
                    {MenuModel.FIELD_APP_NAME,FIELD_APP_NAME},
                    {MenuModel.FIELD_PARENT_MENU_ID,FIELD_PARENT_MENU_ID},
                    {MenuModel.FIELD_CLASS_NAME,FIELD_CLASS_NAME},
                    {MenuModel.FIELD_MENU_ORDER,FIELD_MENU_ORDER},
                    {MenuModel.FIELD_RECORD_COUNT,FIELD_RECORD_COUNT}
                    
                };
                return _menuDAO.GetDynamicListPaging(aModel, pageSize, pageIndex, mapDictionary, compareEqual);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public MenuModel GetMenuById(int? menuId)
        {
            try
            {
                _menuDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {MenuModel.FIELD_MENU_ID,FIELD_MENU_ID},
                    {MenuModel.FIELD_MENU_NAME,FIELD_MENU_NAME},
                    {MenuModel.FIELD_MENU_CODE,FIELD_MENU_CODE},  
                    {MenuModel.FIELD_MENU_DESCRIPTION,FIELD_MENU_DESCRIPTION},
                    {MenuModel.FIELD_MENU_URL,FIELD_MENU_URL},
                    {MenuModel.FIELD_MENU_ORDER,FIELD_MENU_ORDER},
                    {MenuModel.FIELD_PARENT_MENU_ID,FIELD_PARENT_MENU_ID},
                    {MenuModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE},
                    {MenuModel.FIELD_CLASS_NAME,FIELD_CLASS_NAME},
                    {MenuModel.FIELD_APP_ID,FIELD_APP_ID},
                    {MenuModel.FIEL_ROLE_ID,FIELD_ROLE_ID}
                };

                return _menuDAO.GetById(menuId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }


        public bool createOrUpdateMenu(MenuModel menuModel)
        {
            try
            {
                _menuDAO = CreateSQLDAO();
                return _menuDAO.createOrUpdateMenu(menuModel);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool deleteMenu(int[] menuId)
        {
            try
            {
                _menuDAO = CreateSQLDAO();
                return _menuDAO.deleteMenu(menuId);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public List<MenuModel> GetAllMenuByAppId(int appId)
        {
            try
            {
                _menuDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {MenuModel.FIELD_MENU_ID,FIELD_MENU_ID},
                    {MenuModel.FIELD_MENU_NAME,FIELD_MENU_NAME},
                    {MenuModel.FIELD_MENU_CODE,FIELD_MENU_CODE},  
                    {MenuModel.FIELD_MENU_DESCRIPTION,FIELD_MENU_DESCRIPTION},
                    {MenuModel.FIELD_MENU_URL,FIELD_MENU_URL},
                    {MenuModel.FIELD_PARENT_MENU_ID,FIELD_PARENT_MENU_ID},
                    {MenuModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE},
                    {MenuModel.FIELD_CLASS_NAME,FIELD_CLASS_NAME},
                    {MenuModel.FIELD_APP_ID,FIELD_APP_ID},
                    {MenuModel.FIEL_ROLE_ID,FIELD_ROLE_ID}
                };
                return _menuDAO.GetAllMenuByAppId(appId,mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        //phunx1, 20160522: lay danh sach menu theo parentID. 0: lay danh sach menu cha; khac 0 lay danh sach menu con
        public List<MenuModel> GetAllMenuByParentID(int intAppID, int intParentID)
        {
            try
            {
                _menuDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {MenuModel.FIELD_MENU_ID,FIELD_MENU_ID},
                    {MenuModel.FIELD_MENU_NAME,FIELD_MENU_NAME},
                    {MenuModel.FIELD_MENU_CODE,FIELD_MENU_CODE},  
                    {MenuModel.FIELD_MENU_DESCRIPTION,FIELD_MENU_DESCRIPTION},
                    {MenuModel.FIELD_MENU_URL,FIELD_MENU_URL},
                    {MenuModel.FIELD_PARENT_MENU_ID,FIELD_PARENT_MENU_ID},
                    {MenuModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE},
                    {MenuModel.FIELD_CLASS_NAME,FIELD_CLASS_NAME},
                    {MenuModel.FIELD_APP_ID,FIELD_APP_ID},
                    {MenuModel.FIEL_ROLE_ID,FIELD_ROLE_ID}
                };
                return _menuDAO.GetAllMenuByParentID(intAppID, intParentID, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<MenuModel> GetAllMenuByAppCode(string appCode)
        {
            try
            {
                _menuDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {MenuModel.FIELD_MENU_ID,FIELD_MENU_ID},
                    {MenuModel.FIELD_MENU_NAME,FIELD_MENU_NAME},
                    {MenuModel.FIELD_MENU_CODE,FIELD_MENU_CODE},  
                    {MenuModel.FIELD_MENU_DESCRIPTION,FIELD_MENU_DESCRIPTION},
                    {MenuModel.FIELD_MENU_URL,FIELD_MENU_URL},
                    {MenuModel.FIELD_PARENT_MENU_ID,FIELD_PARENT_MENU_ID},
                    {MenuModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE},
                    {MenuModel.FIELD_CLASS_NAME,FIELD_CLASS_NAME},
                    {MenuModel.FIELD_APP_ID,FIELD_APP_ID},
                    {MenuModel.FIEL_ROLE_ID,FIELD_ROLE_ID}
                };
                return _menuDAO.GetAllMenuByAppCode(appCode, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public IEnumerable<MenuModel> GetAllMenuByUserIdAndAppCode(int userId, string appCode)
        {
            try
            {
                _menuDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {MenuModel.FIELD_MENU_ID,FIELD_MENU_ID},
                    {MenuModel.FIELD_MENU_NAME,FIELD_MENU_NAME},
                    {MenuModel.FIELD_MENU_CODE,FIELD_MENU_CODE},  
                    {MenuModel.FIELD_MENU_DESCRIPTION,FIELD_MENU_DESCRIPTION},
                    {MenuModel.FIELD_MENU_URL,FIELD_MENU_URL},
                    {MenuModel.FIELD_PARENT_MENU_ID,FIELD_PARENT_MENU_ID},
                    {MenuModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE},
                    {MenuModel.FIELD_CLASS_NAME,FIELD_CLASS_NAME},
                    {MenuModel.FIELD_APP_ID,FIELD_APP_ID},
                    {MenuModel.FIEL_ROLE_ID,FIELD_ROLE_ID}
                };
                return _menuDAO.GetAllMenuByUserIdAndAppCode(userId,appCode, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool checkMenuExist(int menuId, string menuCode, string menuName,string menuUrl, int appId)
        {
            try
            {
                _menuDAO = CreateSQLDAO();
                return _menuDAO.checkMenuExist(menuId, menuCode, menuName, menuUrl, appId);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool activeMenu(string id)
        {
            try
            {
                _menuDAO = CreateSQLDAO();
                return _menuDAO.activeMenu(id);
            }
            catch
            {
                throw;
            }
        }
    }
}
